<template>
  <div class="login">
     <div class="out">
       <div class="logo">
         <img src="@/assets/images/logo1.jpg" alt="">
       </div>
       <div class="inner">
        <el-form  label-width="120px" :model="loginForm" :rules="loginRules" status-icon ref="loginForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" clearable placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" clearable placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item >
            <el-button type="primary" @click="onSubmit">确认登录</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
       </div>
     </div>
  </div>
</template>

<script>
import { loginAxios } from '@/nextwork/nextworks'
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '密码长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (!valid) {
          this.$message.error('请填写用户名和密码')

          // 发起网络请求
        } else {
          loginAxios(this.loginForm).then(res => {
            if (res.data.code == '401') {
              this.$message.error(res.data.txt)
            } else if (res.data.code == '200') {
              window.sessionStorage.setItem('token', res.data.token)
              this.$router.push('/home')
              this.$message.success(res.data.txt)
            }
          })
        }
      })
    },
    // 重置表单
    onReset () {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login{
  background-color: #0D3F67;
  height: 100%;
  position: relative;
  .out{
    width:460px;
    height: 340px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, .3);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .logo{
      width: 120px;
      height: 120px;
      border-radius: 50%;
      overflow: hidden;
      position: absolute;
      left: calc(50% - 60px);
      top: -60px;
      box-shadow: 0 0 20px rgba(0, 0, 0, .2);
      img{
        width: 100%;
        height: 100%;
      }
    }
    .inner{
      min-height: 200px;
      padding-top: 100px;
      padding-right: 50px;
    }
  }
}
</style>
